import {
	getItem
} from '@/utils/storage.js'
var serverApi = require('@/api/server-api.js')


function Get(url, data, cb, customizeUrl, isShowToast = true) {
	uni.request({
		method: 'GET',
		url: (customizeUrl == null) ? url : customizeUrl,
		header: {
			'Content-Type': 'application/json',
			'Authorization': uni.getStorageSync('atk'),
		},
		data: {
			...data
		},
		success: (res) => {
			typeof cb == "function" && cb(res.data, "");
		},
		fail: (err) => {
			typeof cb == "function" && cb(null, err.errMsg);
			uni.hideNavigationBarLoading();
		}
	});
};

function Post(url, data, cb, customizeUrl) {
	uni.request({
		method: 'POST',
		url: (customizeUrl == null) ? url : customizeUrl,
		header: {
			'Content-Type': 'application/json',
			'Authorization': uni.getStorageSync('atk'),
			
		},
		data: {
			...data
		},
		success: (res) => {
			
			typeof cb == "function" && cb(res.data, "");
		},
		fail: (err) => {
			typeof cb == "function" && cb(null, err.errMsg);

		}
	});
};

function Upload(url, file, data, cb, customizeUrl) {
	uni.uploadFile({
		header: {
			'Authorization': uni.getStorageSync('atk'),
		},
		url: (customizeUrl == null) ? url : customizeUrl,
		filePath: file,
		name: "img",
		formData: {
			...data
		},
		success: (res) => {
			if (typeof(res.data) == "string") {
				typeof cb == "function" && cb(JSON.parse(res.data), "");
			} else if (res.data.returnCode === '429') {
				uni.showToast({
					title: res.data.returnMessage,
					icon: "none"
				})
			} else {
				typeof cb == "function" && cb(res.data, "");
			}

		},
		fail: (err) => {
			typeof cb == "function" && cb(null, err.errMsg);
		}
	});
};

module.exports = {
	Get: Get,
	Post: Post,
	Upload: Upload
};
